﻿package com.tudou.utils
{

    public class URLUtil extends Object
    {

        public function URLUtil()
        {
            return;
        }// end function

        public static function replaceParms(param1:String, param2:String, param3:String) : String
        {
            var _loc_4:* = param1.split("?");
            param1 = param1.split("?").shift();
            var _loc_5:* = _loc_4.join("?");
            var _loc_6:* = _loc_4.join("?").split("&");
            var _loc_7:Array = [];
            var _loc_8:int = 0;
            var _loc_9:* = _loc_6.length;
            while (_loc_8 < _loc_9)
            {
                
                if (param2 == (_loc_6[_loc_8] as String).substr(0, param2.length))
                {
                    _loc_7.push(param2 + "=" + param3);
                }
                else
                {
                    _loc_7.push(_loc_6[_loc_8]);
                }
                _loc_8++;
            }
            return param1 + "?" + _loc_7.join("&");
        }// end function

        public static function getDomainNameFinal(param1:String) : String
        {
            var _loc_2:* = /https?:\/\/([^\/\:]+)""https?:\/\/([^\/\:]+)/i;
            var _loc_3:* = _loc_2.exec(param1);
            if (_loc_3)
            {
                param1 = _loc_3[1];
            }
            return param1;
        }// end function

        public static function isIPAddress(param1:String) : Boolean
        {
            var _loc_2:* = /https?:\/\/(\d{1,3}\.){3}\d{1,3}(:\d{1,5})?\/""https?:\/\/(\d{1,3}\.){3}\d{1,3}(:\d{1,5})?\//ig;
            return _loc_2.test(param1);
        }// end function

        public static function getDomainName(param1:String) : String
        {
            var _loc_2:* = /https?:\/\/([^\/]+)""https?:\/\/([^\/]+)/i;
            var _loc_3:* = _loc_2.exec(param1);
            if (_loc_3)
            {
                param1 = _loc_3[1];
            }
            return param1;
        }// end function

        public static function addParms(param1:String, param2:String) : String
        {
            if (param1.indexOf("?") > 0)
            {
                param1 = param1 + ("&" + param2);
            }
            else
            {
                param1 = param1 + ("?" + param2);
            }
            return param1;
        }// end function

        public static function is51Hot(param1:String) : Boolean
        {
            return param1.match(/51hot""51hot/ig).length > 0;
        }// end function

        public static function getDomain(param1:String) : String
        {
            if (param1 == null || param1 == "")
            {
                return "";
            }
            return param1.match(/http:\/\/.*?\/""http:\/\/.*?\//ig)[0];
        }// end function

        public static function addKeyValue(param1:String, param2:String, param3:String) : String
        {
            return addParms(param1, param2 + "=" + param3);
        }// end function

        public static function replaceKeyValue(param1:String, param2:String, param3:String) : String
        {
            if (param1.indexOf(param2 + "=") === -1)
            {
                trace("addParms(url, key+\"=\"+value);");
                return addParms(param1, param2 + "=" + param3);
            }
            trace("replaceParms(url, key+\"=\"+value);");
            return replaceParms(param1, param2, param3);
        }// end function

    }
}
